package com.hitsz.cdq;

import android.content.Context;
import android.util.Log;

import com.cdq.che.dao.Che_infoDao;
import com.cdq.che.entity.Che_info;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;


/**
 * 完成对某一张表的具体操作
 */
public class CommonUtils {

    //TAG
    private static final String TAG = CommonUtils.class.getSimpleName();

    private DaoManager daoManager;

    //构造方法
    public CommonUtils(Context context) {
        daoManager = DaoManager.getInstance();
        daoManager.initManager(context);
    }

    /**
     * 对数据库中che_info表的插入操作
     *
     * @param che_info
     * @return
     */
    public boolean insertCheInfo(Che_info che_info) {
        boolean flag = false;
        flag = daoManager.getDaoSession().insert(che_info) != -1 ? true : false;
        return flag;
    }

    /**
     * 批量插入
     *
     * @param che_info_list
     * @return
     */
    public boolean inserMultCheInfo(final List<Che_info> che_info_list) {
        //标识
        boolean flag = false;
        try {
            //插入操作耗时
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Che_info che_info : che_info_list) {
                        daoManager.getDaoSession().insertOrReplace(che_info);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改
     *
     * @param che_info
     * @return
     */
    public boolean updateCheInfo(Che_info che_info) {
        boolean flag = false;
        try {
            daoManager.getDaoSession().update(che_info);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除
     *
     * @param che_info
     * @return
     */
    public boolean deleteCheInfo(Che_info che_info) {
        boolean flag = false;
        try {
            //删除指定ID
            daoManager.getDaoSession().delete(che_info);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //daoManager.getDaoSession().deleteAll(); //删除所有记录
        return flag;
    }

    /**
     * 查询单条
     *
     * @param key
     * @return
     */
    public Che_info listOneCheInfo(long key) {
        return daoManager.getDaoSession().load(Che_info.class, key);
    }

    /**
     * 全部查询
     *
     * @return
     */
    public List<Che_info> listAll() {
        return daoManager.getDaoSession().loadAll(Che_info.class);
    }

    /**
     * 原生查询
     */
    public void queryNative() {
        //查询条件
        String where = "where name like ? and _id > ?";
        //使用sql进行查询
        List<Che_info> list = daoManager.getDaoSession().queryRaw(Che_info.class, where,
                new String[]{"%l%", "6"});
        Log.i(TAG, list + "");
    }

    /**
     * QueryBuilder查询大于
     */
    public void queryBuilder() {
        //查询构建器
        QueryBuilder<Che_info> queryBuilder = daoManager.getDaoSession().queryBuilder(Che_info.class);
        //查询年龄大于19的北京
        List<Che_info> list = queryBuilder.where(Che_infoDao.Properties.Id.ge(1)).where(Che_infoDao.Properties.Type.like("01")).list();
        Log.i(TAG, list + "");
    }
}
